﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Text;
using HBKY.Shop.Extensions.ServiceExtensions;
using HBKY.Shop.Extensions.Middleware;
using HBKY.Shop.Common.Helper;

namespace HBKY.Shop.Extensions.CoreBuilder
{
    public class ConfigureBuilder : ICoreConfigurationBuilder
    {
        private readonly IApplicationBuilder _app;
        private readonly IWebHostEnvironment _env;
        private readonly IServiceProvider _svp;
        private readonly IConfiguration _configuration;
        private readonly IServiceCollection _services;
        public ConfigureBuilder(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider svp, IConfiguration configuration, IServiceCollection services)
        {
            _app = app;
            _env = env;
            _svp = svp;
            _configuration = configuration;
            _services = services;
        }

        /// <summary>
        /// 授权
        /// </summary>
        public void UseAuth()
        {
         
        }

        /// <summary>
        /// 跨域
        /// </summary>

        public void UseCors()
        {
            _app.UseCors("AllowDomain");
        }

        /// <summary> 
        /// 异常
        /// </summary>
        public void UseErrorHanle()
        {
          
        }

        /// <summary>
        /// 日志
        /// </summary>
        public void UseNlog()
        {
            ////读取Nlog配置文件
            NLog.LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        }


        /// <summary>
        /// 其他
        /// </summary>
        public void UseOther()
        {
            //_app.UsePathBase("/admin");

            _app.UseCors("AllowDomain");

            _app.UseDeveloperExceptionPage();

            _app.UseRouting();

            _app.UseAuthentication();
            _app.UseAuthorization();

            // 记录请求与返回数据 
            _app.UseHttpLog();

            _app.UseQuartzJob(_env, _svp);

            _app.UseEndpoints(endpoints =>  
            {
                endpoints.MapControllers();
            });

        }

        /// <summary>
        /// 终结
        /// </summary>

        public void UseRazorEngine()
        {
           
        }

        /// <summary>
        /// swagger
        /// </summary>
        public void UseSwagger()
        {
            _app.UseSwaggerSetup();
        }
    }
}
